package cn.telling.product.dao.Impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import cn.telling.common.util.TellingConstance;
import cn.telling.product.dao.IProductSortDao;
import cn.telling.product.vo.ProductSortVo;

@Repository("productSortDao")
public class ProductSortDaoImpl implements IProductSortDao {
    
    private static final Logger LOGGER = LoggerFactory.getLogger(ProductSortDaoImpl.class);

    @Autowired
    @Qualifier("film-template")
    private JdbcTemplate jdbcTemplate;
    
    @Override
    public List<ProductSortVo> getProductSortListByParentId(String parentId) {
        StringBuilder sb = new StringBuilder();
        sb.append("select SORTID,SORTNAME,SORTCODE,SORTNO,PARENTID,SAP_PRODUCT_SORT_CODE from PRODUCT_SORT");
        sb.append(" where PARENTID=" + parentId);
        sb.append(" and DEL_FLAG='" + TellingConstance.DEL_FLAG_1 + "' and system_flag = '01'");
        String sql = sb.toString();
        LOGGER.debug("根据父级类型ID获取产品类型:" + sql);
        List<ProductSortVo> productSortList = jdbcTemplate.query(sql, new RowMapper<ProductSortVo>() {

            @Override
            public ProductSortVo mapRow(ResultSet rs, int rowNum) throws SQLException {
                ProductSortVo productSortVo = new ProductSortVo();
                productSortVo.setSortId(rs.getBigDecimal("SORTID"));
                productSortVo.setSortName(rs.getString("SORTNAME"));
                productSortVo.setSortCode(rs.getString("SORTCODE"));
                productSortVo.setSortNo(rs.getBigDecimal("SORTNO"));
                productSortVo.setParentId(rs.getBigDecimal("PARENTID"));
                productSortVo.setSapProductSortCode(rs.getString("SAP_PRODUCT_SORT_CODE"));
                return productSortVo;
            }
            
        });
        return productSortList;
    }

}
